package cc.messcat.service.fupin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cc.messcat.common.model.Pager;
import cc.messcat.common.service.BaseService;
import cc.messcat.common.service.BaseServiceImpl;
import cc.messcat.common.util.StringUtil;
import cc.messcat.dao.area.AreaDao;
import cc.messcat.dao.fupin.CompanyDao;
import cc.messcat.entity.area.Area;
import cc.messcat.entity.fupin.Company;
import cc.messcat.entity.fupin.PoorFamily;
import cc.messcat.entity.system.McUser;
import cc.messcat.entity.vo.CompanyVo;
import cc.messcat.entity.vo.PoorFamilyVo;

@Service
public class CompanyServiceImpl extends BaseServiceImpl<Company> implements CompanyService {

    @Autowired
    private AreaDao areaDao;

    @Autowired
    private CompanyDao companyDao;

    @Override
    public Pager getCompanyPager(Integer pageNo, Integer pageSize, String company_name, String legal_person, String cry,
                                 String village, String string, McUser user) {
        Map map = new HashMap<>();
        StringBuffer sb = new StringBuffer();
        String sql = "SELECT c.id, c.country,c.village,c.company_dm,c.company_name,c.legal_person,c.registered_fund,c.tel,c.active_staff,c.qzpk,c.qzfpk,c.lspk,c.lsfpk,c.business_item,c.company_profile,c.link ";
//        String sql = "SELECT c.id, c.country,c.village,c.company_dm,c.company_name,c.legal_person,c.registered_fund,c.tel,c.active_staff,c.qzpk,c.qzfpk,c.lspk,c.lsfpk ";

        sb.append(" FROM company c  WHERE c.is_del = '0' ");


        if (StringUtil.isNotBlank(company_name)) {
            sb.append(" and c.company_name like '%" + company_name + "%'");
        }

        if (StringUtil.isNotBlank(legal_person)) {
            sb.append(" and c.legal_person like '%" + legal_person + "%'");
        }

        if (StringUtil.isNotBlank(cry)) {
            Area area = areaDao.getAreaByCode(cry);
            sb.append(" and c.country =" + area.getArea());
        }

        if (StringUtil.isNotBlank(village)) {
            Area area = areaDao.getAreaByCode(village);
            sb.append(" and c.village =" + area.getArea());
        }
        sb.append(" ORDER BY c.create_time");

        int rowCount = companyDao.getCount("select count(*)" + sb.toString(), map);

        List<CompanyVo> CompanyVoList = companyDao.getCompanyVo(pageNo, pageSize, sql + sb.toString(), map);

        Pager<CompanyVo> page = new Pager<CompanyVo>(pageSize, pageNo, rowCount, CompanyVoList);

        return page;
    }

    @Override
    public Pager<CompanyVo> queryCompanyFamilyById(String id, Integer pageNo, Integer pageSize) {
        Map map = new HashMap<>();
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT c.id,c.province,c.city,c.county, c.country,c.village,c.street,c.stockholder_number,c.map_coordinates_x,c.map_coordinates_y,c.business_scope,c.business_item,c.company_profile,c.honor,c.legal_person_pic,pf.poor_family_id,pf.family_id,"
                + " c.company_dm,c.company_name,c.legal_person,c.registered_fund,c.tel,c.active_staff,c.qzpk,c.qzfpk,c.lspk,c.lsfpk,"
                + " pf.job_type,pf.work_type,pf.monthly_salary,pf.work_time,f.name  ");
        String sql2 = " FROM company c LEFT JOIN company_poorfamily pf ON c.id = pf.company_id LEFT JOIN  family f ON pf.family_id = f.id WHERE c.is_del = '0' AND pf.is_del = '0'  AND f.is_del = '0' AND c.id = " + id;

        List<CompanyVo> CompanyVoList = companyDao.queryCompanyFamilyById(sql.toString() + sql2);


        if (null != CompanyVoList && CompanyVoList.size() > 0) {
            for (CompanyVo companyVo : CompanyVoList) {
                String job_type = companyVo.getJob_type();
                if ("1".equals(job_type)) {
                    job_type = "全职贫困户";
                }
                if ("2".equals(job_type)) {
                    job_type = "临时贫困户";
                }
                if ("3".equals(job_type)) {
                    job_type = "全职非贫困户";
                }
                if ("4".equals(job_type)) {
                    job_type = "临时非贫困户";
                }
                companyVo.setJob_type(job_type);
            }
        }

        int rowCount = companyDao.getCount("select count(*)" + sql2, map);

        Pager<CompanyVo> page = new Pager<CompanyVo>(pageSize, pageNo, rowCount, CompanyVoList);

        return page;
    }

    @Override

    public void delCompanyById(String id) {
        //删除企业信息
        StringBuffer sql = new StringBuffer();
        sql.append("update company set is_del = '1' where id =" + id);
        companyDao.executeUpdateById(sql.toString());


        StringBuffer sql2 = new StringBuffer();
        sql2.append("update company_poorfamily set is_del = '1' where company_id =" + id);
        companyDao.executeUpdateById(sql.toString());

    }

    @Override
    public void addCompany(Company company) {
        companyDao.save(company);
    }

    @Override
    public void updateCompany(Company company) {
        company.setIs_del("0");
        companyDao.update(company);
    }

    @Override
    public Company queryCompanyById(String id) {
        long companyId = Long.parseLong(id);
        return companyDao.get(companyId);
    }

    @Override
    public int checkCompanyName(String company_name) {
        Map map = new HashMap<>();
        //map.put("company_name",company_name);
        String sql = "select count(*) from company where company_name = '" + company_name + "'";

        int rowCount = companyDao.getCount(sql, map);
        return rowCount;
    }

    ;

}
